package com.nic.view.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.nic.view.R;

import java.util.List;


/**
 * Created by Administrator on 2019/4/11.
 */

public class GridChooseDialog extends Dialog {
    private View dialogView;
    private TextView tv_title;
    private GridView gv;
    private Button cancel;

    public GridChooseDialog(@NonNull Context context) {
        super(context);
        dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_grid_choose, null);
        // 设置点击对话框外部不能取消
        setCanceledOnTouchOutside(false);
        // 加载对话框文件
        setContentView(dialogView);
        setAttributes(((Activity) context).getWindowManager());
        tv_title = (TextView) dialogView.findViewById(R.id.tv_title);
        gv = (GridView) dialogView.findViewById(R.id.gv);
        cancel = (Button) dialogView.findViewById(R.id.cancel);//不选

        //列表
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                view.setBackgroundColor(context.getResources().getColor(R.color.gray));
                if (listener != null) {
                    listener.onItemClick(parent, view, position, id);
                }
            }
        });
        //不选
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(v);
                }
            }
        });

    }

    /**
     * 设置对话框的大小
     *
     * @param windowManager 窗口管理器
     */
    private void setAttributes(WindowManager windowManager) {
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.width = (int) (display.getWidth() * 0.95); //设置宽度
        //      lp.height = (int) (display.getHeight() * 0.9); //设置高度
        this.getWindow().setAttributes(lp);
    }

    /**
     * 设置数据
     */
    public void setData(List<String> mLists) {
        if (mLists == null || mLists.isEmpty()) {
            return;
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), R.layout.customer_text_view, mLists);
        gv.setAdapter(adapter);
    }

    /**
     * 设置标题
     */
    public void setTitle(String title) {
        tv_title.setText(title);
    }

    private OnGridChooseDialogListener listener;

    public void setGridChooseDialogListener(OnGridChooseDialogListener listener) {
        this.listener = listener;
    }

    public interface OnGridChooseDialogListener {
        void onItemClick(AdapterView<?> parent, View view, int position, long id);

        void onClick(View v);
    }

}
